                PRESERVE8      ; fix the L6306W waring
                AREA |.text|, CODE, READONLY
/**
 * function [ mx_atom_swp ] implementation
 * @prototype: mx_usize_t mx_atom_swp (mx_usize_t newv, volatile mx_usize_pt var);
 */
mx_atom_swp     \
                PROC
                EXPORT mx_atom_swp
                push {r3, r2}
__loop
                ldrex r2, [r1]              ; r2 = var                          // hardware: mark the `var`
                strex r3, r0, [r1]          ; if (var modified) { r3 = 1; }
                                            ; else { r3 = 0; var = newv; }      // hardware: clear the mark of `var`
                cmp r3, #1                  ; if (r3 == 0)
                beq __loop                  ; { goto __loop }
                mov r0, r2
                pop {r3, r2}
                mov pc, lr                  ; return r0
                ENDP

                END

/**
 * note: ldrex and strex instructions can ensure that mutually exclusive access variable between THREAD and HANDLER mode of CPU,
 *       we needn't care about `mx_thread_switch` because function can't be split in such a non-interrupt context switch scenario.
 *       Please see `questions.md` for more information about ldrex and strex instructions.
 */
